<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="JavaScript类型转换​        本文参考文章 1https:&#x2F;&#x2F;developer.mozilla.org&#x2F;zh-CN&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Equality_comparisons_and_sameness    非严格相等判断中的隐式类型转换​        因为JavaScript的非严格相等，所以存在在比较时进行类型转换。 转换规则：  null 和 und">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript类型转换">
<meta property="og:url" content="http://tallgy.gitee.io/2022/03/15/JavaScript/JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="JavaScript类型转换​        本文参考文章 1https:&#x2F;&#x2F;developer.mozilla.org&#x2F;zh-CN&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Equality_comparisons_and_sameness    非严格相等判断中的隐式类型转换​        因为JavaScript的非严格相等，所以存在在比较时进行类型转换。 转换规则：  null 和 und">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20220315133546860.png">
<meta property="article:published_time" content="2022-03-15T02:01:43.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.323Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="随笔">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tallgy.gitee.io/.io//image-20220315133546860.png">


<link rel="canonical" href="http://tallgy.gitee.io/2022/03/15/JavaScript/JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2022/03/15/JavaScript/JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/","path":"2022/03/15/JavaScript/JavaScript类型转换/","title":"JavaScript类型转换"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaScript类型转换 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">1.</span> <span class="nav-text">JavaScript类型转换</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9D%9E%E4%B8%A5%E6%A0%BC%E7%9B%B8%E7%AD%89%E5%88%A4%E6%96%AD%E4%B8%AD%E7%9A%84%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">1.1.</span> <span class="nav-text">非严格相等判断中的隐式类型转换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%A0%E4%B8%AA%E6%A1%88%E4%BE%8B%E6%B5%8B%E8%AF%95%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">几个案例测试：</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/03/15/JavaScript/JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaScript类型转换
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-15 10:01:43" itemprop="dateCreated datePublished" datetime="2022-03-15T10:01:43+08:00">2022-03-15</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/%E9%9A%8F%E7%AC%94/" itemprop="url" rel="index"><span itemprop="name">随笔</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="JavaScript类型转换"><a href="#JavaScript类型转换" class="headerlink" title="JavaScript类型转换"></a>JavaScript类型转换</h1><p>​        本文参考文章</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Equality_comparisons_and_sameness</span><br></pre></td></tr></table></figure>



<h2 id="非严格相等判断中的隐式类型转换"><a href="#非严格相等判断中的隐式类型转换" class="headerlink" title="非严格相等判断中的隐式类型转换"></a>非严格相等判断中的隐式类型转换</h2><p>​        因为JavaScript的非严格相等，所以存在在比较时进行类型转换。</p>
<p>转换规则：</p>
<ul>
<li><p>null 和 undefined 互为相等。和基本数据类型为false，引用数据类型通过 isFalsy 来进行判断（Falsy代表的是在Boolean属性中被认为是false的值）</p>
</li>
<li><p>其他类型和number的比较，和基本数据类型会转为数字，引用类型则通过ToPrimitive转换为基本类型然后再进行比较。</p>
<ul>
<li><p>```<br><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive</a></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* ```</span><br><span class="line">  具体的我们后续再继续了解。</span><br><span class="line">  </span><br><span class="line">  let a = &#123;</span><br><span class="line">    value: 1,</span><br><span class="line">    [Symbol.toPrimitive](hint) &#123;</span><br><span class="line">      console.log(&#x27;hint&#x27;, hint)</span><br><span class="line">  </span><br><span class="line">      if (hint == &#x27;number&#x27;) return this.value</span><br><span class="line">      if (hint == &#x27;string&#x27;) return &#x27;2&#x27;</span><br><span class="line">      return true</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;</span><br><span class="line">  let b = 12</span><br><span class="line">  </span><br><span class="line">  console.log(a == b)</span><br></pre></td></tr></table></figure></li>
<li><p>我们现在就先知道这个 toPrimitive 的一个作用是什么就好。他会将对象转为一个基本的数据类型。对于一个对象，会先通过 toPrimitive 调用 valueOf 方法转换为 基本数据类型，如果不能，那么便通过toString 转换为基本数据类型，否则便会报错。但是对于date 类型就会反过来进行调用。（有人说是因为date的数值转化较大，所以没有意义转化为数值）</p>
</li>
</ul>
</li>
<li><p>布尔类型的比较，其他类型都转化为number，对象使用ToPrimitive，同时布尔转化为number</p>
</li>
</ul>
<p>所以大致一个转化的思路就是</p>
<p>undefined 和 null 将为 空。</p>
<p>引用类型和其他类型会通过primitive转化为基本类型</p>
<p>其他的有number，以number为主。</p>
<p>布尔和string要转化为number</p>
<p>有一个图也进行了说明：</p>
<img src="/.io//image-20220315133546860.png" alt="image-20220315133546860" style="zoom:67%;">



<table>
<thead>
<tr>
<th></th>
<th></th>
<th>B</th>
<th>被</th>
<th>比</th>
<th>较</th>
<th>值</th>
<th>B</th>
</tr>
</thead>
<tbody><tr>
<td>A</td>
<td></td>
<td>Undefined</td>
<td>Null</td>
<td>Number</td>
<td>String</td>
<td>Boolean</td>
<td>Object</td>
</tr>
<tr>
<td>被</td>
<td>Undefined</td>
<td><code>true</code></td>
<td><code>true</code></td>
<td><code>false</code></td>
<td><code>false</code></td>
<td><code>false</code></td>
<td><code>IsFalsy(B)</code></td>
</tr>
<tr>
<td>比</td>
<td>Null</td>
<td><code>true</code></td>
<td><code>true</code></td>
<td><code>false</code></td>
<td><code>false</code></td>
<td><code>false</code></td>
<td><code>IsFalsy(B)</code></td>
</tr>
<tr>
<td>较</td>
<td>Number</td>
<td><code>false</code></td>
<td><code>false</code></td>
<td><code>A === B</code></td>
<td><code>A === ToNumber(B)</code></td>
<td><code>A=== ToNumber(B)</code></td>
<td><code>A== ToPrimitive(B)</code></td>
</tr>
<tr>
<td>值</td>
<td>String</td>
<td><code>false</code></td>
<td><code>false</code></td>
<td><code>ToNumber(A) === B</code></td>
<td><code>A === B</code></td>
<td><code>ToNumber(A) === ToNumber(B)</code></td>
<td><code>ToPrimitive(B) == A</code></td>
</tr>
<tr>
<td>A</td>
<td>Boolean</td>
<td><code>false</code></td>
<td><code>false</code></td>
<td><code>ToNumber(A) === B</code></td>
<td><code>ToNumber(A) === ToNumber(B)</code></td>
<td><code>A === B</code></td>
<td><code>ToNumber(A) == ToPrimitive(B)</code></td>
</tr>
<tr>
<td></td>
<td>Object</td>
<td><code>false</code></td>
<td><code>false</code></td>
<td><code>ToPrimitive(A) == B</code></td>
<td><code>ToPrimitive(A) == B</code></td>
<td><code>ToPrimitive(A) == ToNumber(B)</code></td>
<td><code>A === B</code></td>
</tr>
</tbody></table>
<h2 id="几个案例测试："><a href="#几个案例测试：" class="headerlink" title="几个案例测试："></a>几个案例测试：</h2><p>​        这个是我笔试的时候遇到的题目案例：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">console.log([] == ![])</span><br><span class="line">	对象和布尔之间的。第二个 ![] 属于 false</span><br><span class="line">	所以就是 [] 和 false 的比较，[] toString &#x27;&#x27;</span><br><span class="line">	然后字符串和布尔的比较，转number，都为 0</span><br><span class="line">	所以为 true</span><br><span class="line"></span><br><span class="line">console.log([&#x27;&#x27;] == false)</span><br><span class="line">	第二个一样：[&#x27;&#x27;] 的 toString 为 &#x27;&#x27;</span><br><span class="line">	所以也为true</span><br><span class="line">	</span><br><span class="line">console.log([] !== [])</span><br><span class="line">	对象的严格比较，直接比较地址的引用是否为同一个地址。</span><br><span class="line">	所以也为true</span><br><span class="line"></span><br><span class="line">console.log([&#x27;&#x27;] == [])</span><br><span class="line">	对象的比较，直接比较地址</span><br><span class="line">	false</span><br></pre></td></tr></table></figure>










    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E9%9A%8F%E7%AC%94/" rel="tag"># 随笔</a>
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/03/14/JavaScript/String%E6%96%B9%E6%B3%95/" rel="prev" title="String方法">
                  <i class="fa fa-chevron-left"></i> String方法
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/03/21/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/1-%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C/" rel="next" title="1-两数之和">
                  1-两数之和 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
